package com.atguigu.tingshu.order.strategy;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 保存订单策略接口
 */
public interface SaveOrderStrategy {

    // 保存订单方法
    public abstract OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) ;

    // 获取策略实现类对应的支付方式
    public abstract String getPayWay() ;

    default public void saveOrderInfoAndDrateInfoAndDetailInfo(OrderInfoVo orderInfoVo,
                                                               OrderInfoService orderInfoService,
                                                               OrderDerateService orderDerateService,
                                                               OrderDetailService orderDetailService) {

        // 获取当前登录的用户
        Long userId = AuthContextHolder.getUserId();

        // 1、保存订单的基本数据
        OrderInfo orderInfo = new OrderInfo() ; // 订单基本数据
        orderInfo.setUserId(userId);

        // 获取订单的明细数据
        List<OrderDetailVo> orderDetailVoList = orderInfoVo.getOrderDetailVoList();
        String itemName = orderDetailVoList.get(0).getItemName();
        orderInfo.setOrderTitle(itemName);
        orderInfo.setOrderNo(orderInfoVo.getTradeNo());
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
        orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
        orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
        orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
        orderInfo.setItemType(orderInfoVo.getItemType());
        orderInfo.setPayWay(orderInfoVo.getPayWay());
        orderInfoService.save(orderInfo) ;

        // 2、保存订单的明细数据
        List<OrderDetail> orderDetailList = orderDetailVoList.stream().map(orderDetailVo -> {
            OrderDetail od = new OrderDetail();
            od.setOrderId(orderInfo.getId());
            od.setItemId(orderDetailVo.getItemId());
            od.setItemName(orderDetailVo.getItemName());
            od.setItemUrl(orderDetailVo.getItemUrl());
            od.setItemPrice(orderDetailVo.getItemPrice());
            return od;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderDetailList) ;

        // 3、保存订单减免明细数据
        List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
        if(!CollectionUtils.isEmpty(orderDerateVoList)) {
            List<OrderDerate> orderDerateList = orderDerateVoList.stream().map(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                orderDerate.setOrderId(orderInfo.getId());
                orderDerate.setDerateType(orderDerateVo.getDerateType());
                orderDerate.setDerateAmount(orderDerateVo.getDerateAmount());
                orderDerate.setRemarks(orderDerateVo.getRemarks());
                return orderDerate;
            }).collect(Collectors.toList());
            orderDerateService.saveBatch(orderDerateList) ;
        }
        // 执行后续操作
        this.saveOrderProcess(orderInfoVo.getTradeNo());

    }


    // 定义保存完订单数据以后，后续操作的抽象方法
    public abstract void saveOrderProcess(String orderNo) ;
}
